import path               from 'path';
import dotenv             from 'dotenv';
import webpack            from 'webpack';
import HtmlPlugin         from 'html-webpack-plugin';
import utils              from './bins/utils';

// load base config
dotenv.config();
const env               = process.env;

/****************************************************
 * config entry
 ***************************************************/
const entry             = {};

// ./src/App.ts
entry.generated               = [
  `.${path.sep}${env.DIRECTORY_SOURCE}${path.sep}${env.FILE_SOURCE_MAIN}`,
  `.${path.sep}${env.DIRECTORY_SOURCE}${path.sep}${env.DIRECTORY_SOURCE_MODULES}${path.sep}${env.FILE_SOURCE_COMMON}`
];

/****************************************************
 * config output
 ***************************************************/
const output            = {};
output.publicPath       = './';
output.path             = path.join(__dirname, env.DIRECTORY_OUTPUT);
output.filename         = `${env.DIRECTORY_OUTPUT_STATIC}/js/[name].js`

if ( 'development' === env.NODE_ENV ) {
  output.path           = path.join(__dirname, env.DIRECTORY_DEVELOP);
  output.publicPath     = '/';
}

if( 'server' === env.BUILD_TYPE ) {
  output.path           = path.join(__dirname, env.DIRECTORY_SERVICE);
  output.libraryTarget  = 'commonjs2';
}


/****************************************************
 * config resolve
 ***************************************************/
const resolve           = {
  extensions        : ['.ts', '.tsx', '.js', '.jsx'],
  alias: {
    'src'           : path.resolve('src'),
    'assets'        : path.resolve('src/assets'),
    'styles'        : path.resolve('src/styles'),
    'config'        : path.resolve('src/config'),
    'components'    : path.resolve('src/components'),
    'containers'    : path.resolve('src/containers'),
  }
};

/****************************************************
 * config module
 ***************************************************/
const module            = {
  rules: []
};

//typescript
//----------------
// hot
//if ( 'development' === env.NODE_ENV ) {
//  module.rules.push({
//    test: /\.(ts|tsx)$/,
//    exclude: /node_modules/,
//    loader: 'react-hot-loader'
//  });
//}
// ts
module.rules.push({
  test: /\.(ts|tsx)$/,
  exclude: /node_modules/,
  loader: 'awesome-typescript-loader',
  include: [path.resolve('src')]
});

// tslint
module.rules.push({
  test: /\.(ts|tsx)$/,
  enforce: 'pre',
  exclude: /node_modules/,
  loader: 'tslint-loader',
  include: [path.resolve('src')]
});

/****************************************************
 * config plugins
 ***************************************************/
const plugins           = [];

plugins.push( new HtmlPlugin(utils.getConfigHtmlTemplate()) );
plugins.push( new webpack.DefinePlugin(utils.getConfigDefinePlugin()) );
plugins.push( new webpack.optimize.CommonsChunkPlugin(utils.getConfigCommons()));
if ( 'production' === env.NODE_ENV ) {
  plugins.push( new webpack.optimize.UglifyJsPlugin(utils.getConfigOptimize()));
}

/****************************************************
 * config target
 ***************************************************/
let target              = 'web';

if( 'server' === env.BUILD_TYPE ) {
  target                = 'node';
}

export default { entry, output, module, resolve, plugins, target };
